import modelExtend from 'dva-model-extend'
import { create, remove, update,createGroup,listAllGroups,createTag,listAllTags } from '../services/fans'
import * as fanssService from '../services/fanss'
import { pageModel } from './common'
import { config } from '../utils'
import { message, Button } from 'antd'

const { query } = fanssService
const { prefix } = config

export default modelExtend(pageModel, {
  namespace: 'fans',

  state: {
    currentItem: {},
    modalVisible: false,
    chatModalVisible:false,
    modalType: 'create',
    chatModalType:'create',
    selectedRowKeys: [],
    isMotion: localStorage.getItem(`${prefix}fansIsMotion`) === 'true',
    // 组
    groupVisible:false,
    selectGroupVisible:false,
    groupName:'',
    groupList:[],
    checkedGroupList:[],
    // 标签
    tagVisible:false,
    selectTagVisible:false,
    tagName:'',
    tagList:[],
    checkedTagList:[],
  },
  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/fans') {
        dispatch({
          type: 'query',
          payload: location.query,
        })
      }
    })
    },
  },

  effects: {

  *query ({ payload = {} }, { call, put }) {
  debugger
  const data = yield call(query, payload)
  if (data) {
    yield put({
      type: 'querySuccess',
      payload: {
        list: data.data,
        pagination: {
          current: Number(payload.page) || 1,
          pageSize: Number(payload.pageSize) || 10,
          total: data.total,
        },
      },
    })
  }
},

*'delete' ({ payload }, { call, put, select }) {
  const data = yield call(remove, { id: payload })
  const { selectedRowKeys } = yield select(_ => _.fans)
  if (data.success) {
    yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
    yield put({ type: 'query' })
  } else {
    throw data
  }
},

*'multiDelete' ({ payload }, { call, put }) {
  const data = yield call(fanssService.remove, payload)
  if (data.success) {
    yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
    yield put({ type: 'query' })
  } else {
    throw data
  }
},

*create ({ payload }, { call, put }) {
  const data = yield call(create, payload)
  if (data.success) {
    yield put({ type: 'hideModal' })
    yield put({ type: 'query' })
  } else {
    throw data
  }
},

*update ({ payload }, { select, call, put }) {
  const id = yield select(({ fans }) => fans.currentItem.id)
  const newFans = { ...payload, id }
  const data = yield call(update, newFans)
  if (data.success) {
    yield put({ type: 'hideModal' })
    yield put({ type: 'query' })
  } else {
    throw data
  }
},
    // 组管理
    // 创建组
*createGroup ({ payload }, { call, put }) {
  debugger
  const data = yield call(createGroup, payload)
  if (data.success) {
    debugger
    yield put({ type: 'hideGroup' })
    if(data.status === '000000') {
      message.success('新建组成功');
    } else {
      message.error(data.memo);
    }
  } else {
    throw data
  }
},
    // 获取所有组
    *listAllGroups ({ payload }, { call, put }) {
      debugger
      const data = yield call(listAllGroups, payload)
      let groupListData = []

      if (data.success) {
        debugger
        data.data.map(function(value){
          let item = {label:value.name,value:value.name}
          groupListData.push(item)
        })

        yield put({ type: 'showSelectGroup', payload: {groupList: groupListData} })
        if(data.status !== '000000') {
          message.error(data.memo);
        }
      } else {
        throw data
      }
    },

    // 标签管理
    // 创建标签
    *createTag ({ payload }, { call, put }) {
      debugger
      const data = yield call(createTag, payload)
      if (data.success) {
        debugger
        yield put({ type: 'hideTag' })
        if(data.status === '000000') {
          message.success('新建标签成功');
        } else {
          message.error(data.memo);
        }
      } else {
        throw data
      }
    },
    // 获取所有组
    *listAllTags ({ payload }, { call, put }) {
      debugger
      const data = yield call(listAllTags, payload)
      let tagListData = []

      if (data.success) {
        debugger
        data.data.map(function(value){
          let item = {label:value.name,value:value.name}
          tagListData.push(item)
        })

        yield put({ type: 'showSelectTag', payload: {groupList: tagListData} })
        if(data.status !== '000000') {
          message.error(data.memo);
        }
      } else {
        throw data
      }
    },
},

reducers: {

  showModal (state, { payload }) {
    return { ...state, ...payload, modalVisible: true }
  },

  hideModal (state) {
    return { ...state, modalVisible: false }
  },

  switchIsMotion (state) {
    localStorage.setItem(`${prefix}fansIsMotion`, !state.isMotion)
    return { ...state, isMotion: !state.isMotion }
  },
  showChatModal (state, { payload }) {
    return { ...state, ...payload, chatModalVisible: true }
  },
  hideChatModal (state, { payload }) {
    return { ...state, ...payload, chatModalVisible: false }
  },
  hideGroup (state, { payload }) {
    return { ...state, ...payload, groupVisible: false }
  },
  showGroup (state, { payload }) {
    return { ...state, ...payload, groupVisible: true }
  },
  showSelectGroup (state, { payload }) {
    return { ...state, ...payload, selectGroupVisible: true }
  },
  hideSelectGroup (state, { payload }) {
    debugger
    return { ...state, ...payload, selectGroupVisible: false }
  },

  // 标签管理
  hideTag (state, { payload }) {
    return { ...state, ...payload, tagVisible: false }
  },
  showTag (state, { payload }) {
    return { ...state, ...payload, tagVisible: true }
  },
  showSelectTag (state, { payload }) {
    return { ...state, ...payload, selectTagVisible: true }
  },
  hideSelectTag (state, { payload }) {
    debugger
    return { ...state, ...payload, selectTagVisible: false }
  },
},
})
